package com.ln.lnzw.activity;

import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.ln.lnzw.R;
import com.ln.lnzw.adapter.AnnouncementAdapter;
import com.ln.lnzw.adapter.MineCollectionAdapter;
import com.ln.lnzw.app.AppConstant;
import com.ln.lnzw.base.BaseActivity;
import com.ln.lnzw.bean.MineCollectionNetbean;
import com.ln.lnzw.bean.WcmBean;
import com.ln.lnzw.net.HttpMethods;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者 : wlt
 * 创建时间:  2018-05-04
 * QQ : 1346947404
 * 邮箱:wanglt_android@163.com
 * 类说明:
 * In me the tiger sniffs the rose
 */

public class MineCollectionActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rv_recyclerview)
    RecyclerView rvRecyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.tv_name)
    TextView tvName;

    Unbinder unbinder;

    private MineCollectionAdapter mAdapter;
    private int count = 1;
    private int limit = 10;
    private int lastSize = 0;
    private int nowSize = 0;
    private int delayMillis = 1000;
    private List<MineCollectionNetbean.ResultBean.ListBean> mList = new ArrayList<>();
    private String moreUrl;
    private String name;
    private String userId ;
    private String token;
    private static final String TAG = "MineCollectionActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_announcement_more);
        unbinder = ButterKnife.bind(this);
        setView();
        initData();
    }

    private void initData(){
        userId = activity.spUtils.getString(AppConstant.USER_UID);
        token = activity.spUtils.getString(AppConstant.TOKEN);
        HttpMethods.getInstanceCenter().info.getMineCollectionList(userId,count,limit,token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MineCollectionNetbean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(MineCollectionNetbean mineCollectionNetbean) {
                        if (!mList.isEmpty()){mList.clear();}
                            if (mineCollectionNetbean.getResult() != null && mineCollectionNetbean.getResult().getList() != null){
                                mList.addAll(mineCollectionNetbean.getResult().getList());
                                lastSize = mList.size();
                            }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "获取我的收藏列表失败 "+e.toString() );
                        dispose();
                    }

                    @Override
                    public void onComplete() {
                        mAdapter.notifyDataSetChanged();
                    }
                });
    }
    private void setView(){
        name = getIntent().getStringExtra("name");
        tvName.setText(name);
        rvRecyclerview.setNestedScrollingEnabled(false);
        rvRecyclerview.setLayoutManager(new LinearLayoutManager(activity));
        mAdapter = new MineCollectionAdapter(R.layout.item_announcement, mList);
        rvRecyclerview.setAdapter(mAdapter);
        refreshLayout.setDisableContentWhenRefresh(true);//是否在刷新的时候禁止列表的操作
        refreshLayout.setDisableContentWhenLoading(true);//是否在加载的时候禁止列表的操作
        //设置下拉刷新监听事件
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
                downRefresh();
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                onLoadMoreRequested();
            }
        });
    }

    /**
     * 下拉刷新
     */
    public void downRefresh() {
        count = 1;
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setNoMoreData(false);
                initData();
                refreshLayout.finishRefresh();
            }
        }, delayMillis);
    }
    /**
     * 上拉加载
     */
    public void onLoadMoreRequested() {
        userId = activity.spUtils.getString(AppConstant.USER_UID);
        token = activity.spUtils.getString(AppConstant.TOKEN);
        HttpMethods.getInstanceCenter().info.getMineCollectionList(userId,count+1,limit,token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MineCollectionNetbean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(MineCollectionNetbean mineCollectionNetbean) {

                        if (mineCollectionNetbean.getResult().getList() != null){
                            mList.addAll(mineCollectionNetbean.getResult().getList());
                            nowSize = mList.size();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "获取我的收藏列表失败 "+e.toString() );
                        dispose();
                        refreshLayout.finishLoadMore(500);
                    }

                    @Override
                    public void onComplete() {
                        if (nowSize>lastSize){
                            mAdapter.notifyDataSetChanged();
                            count=count+1;
                        }else {
                            ToastUtils.showShort("没有更多数据");
                        }
                        refreshLayout.finishLoadMore(500);
                    }
                });

    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        dispose();
    }
}
